package com.px.chapter_Dec_1.logistics;

public class Box<T> {
    private T arr[];
    private int a=0;

    public Box() {
        this(5);
    }

    public Box(int size) {
        this.arr= (T[]) new Object[size];
    }
    public synchronized void add(T ss ) {
        while(a==arr.length){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        arr[a]=ss;
        a++;
        this.notify();

    }
    public synchronized T  take()  {
        while (a==0){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        try {
            Thread.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        a--;
        T aa=arr[a];

        this.notify();
        return aa;
    }
    public int count(){
        return a;

    }
}


